<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Channel extends Stourweb_Controller
{
    public $_supplier_id;
    public $_user_info;

    public function before()
    {
        parent::before();
        //登录状态判断
        $this->_supplier_id = Cookie::get('st_supplier_id');
        if(empty($this->_supplier_id))
        {
            $this->request->redirect('pc/login');
        }
        else
        {
            $this->_user_info = Model_Supplier::get_supplier_byid($this->_supplier_id);
            $this->assign('userinfo',$this->_user_info);
        }
        $this->assign('web_url','/supplier/pc/channel/');
    }

    /**
     * 分销控制页面
     */
    public function action_control()
    {
        $supplier = DB::select()->from('supplier')->where("id={$this->_supplier_id}")->execute()->current();
        $authorization = $supplier['authorization'];
        $supplier_channel = explode(',',$supplier['channel_id']);
        $channel = Model_Supplier::get_ota_channel($this->_supplier_id,true);

        $arr = array(1,2,5,114);
        /*不供应平台时，unset掉不支持的应用*/
        if(!in_array(1,$supplier_channel)){
            $authorization = explode(',',$authorization);
            foreach ($authorization as $k=>$v){
                if(!in_array($v,$arr)){
                    unset($authorization[$k]);
                }
            }
            $authorization = implode(',',$authorization);
        }

        if($authorization){
            $app_list = DB::select()->from('model')->where("id IN($authorization)")->execute()->as_array();
            foreach ($app_list as &$v){
                $v['class_name'] = !in_array($v['id'],$arr)?'channel_none':'channel_show';
            }
            $this->assign('app_list',$app_list);
        }
        $this->assign('channel',$channel);
        $this->display('channel/control');
    }

    /**
     * 渠道产品信息
     */
    public function action_product_list()
    {
        $params = array(
            'supplier_id' => $this->_supplier_id,
            'type_id'     => Common::remove_xss(Arr::get($_POST,'type_id')),
            'page'        => Common::remove_xss(Arr::get($_POST,'page', 1)),
            'page_size'   => Common::remove_xss(Arr::get($_POST,'page_size', 13)),
            'keyword'     => Common::remove_xss(Arr::get($_POST,'keyword')),
            'channel_id'  => Common::remove_xss(Arr::get($_POST,'channel_id')),
        );
        if(!$params['type_id']){
            exit(json_encode(array('status'=>false,'msg'=>'你没有权限进行此项操作，请联系管理员！')));
        }
        $table_field = St_Functions::table_field($params['type_id']);
        $model = 'Model_Supplier_Product_Price_'.ucfirst($table_field[3]);
        $res = call_user_func_array(array($model, 'channel_product_list'), array('params'=>json_encode($params)));

        echo json_encode($res);
    }

    /**
     * 产品上下架控制
     */
    public function action_product_operation()
    {
        $params = array(
            'type_id'        => Common::remove_xss(Arr::get($_POST,'type_id')),
            'product_id'     => Common::remove_xss(Arr::get($_POST,'product_id')),
            'ishidden'       => Common::remove_xss(Arr::get($_POST,'ishidden')),
            'status'         => Common::remove_xss(Arr::get($_POST,'status')),
            'channel_id'     => Common::remove_xss(Arr::get($_POST,'channel_id')),
            'channel_status' => Common::remove_xss(Arr::get($_POST,'channel_status')),
        );
        if(!$params['type_id'] || !$params['product_id']){
            exit(json_encode(array('status'=>false,'msg'=>'你没有权限进行此项操作，请联系管理员！')));
        }
        $table_field = St_Functions::table_field($params['type_id']);
        if($params['type_id']==5){
            $product = DB::select()->from($table_field[0])->where("id={$params['product_id']}")->execute()->current();
        }else{
            $product = DB::select()->from($table_field[3])->where("id={$params['product_id']}")->execute()->current();
        }
        if($params['channel_id']>1){
            $supplier_channel_id = explode(',',$product['supplier_channel_id']);
            if($params['channel_status']=='false'){
                array_push($supplier_channel_id,$params['channel_id']);
            }else{
                unset($supplier_channel_id[array_search($params['channel_id'],$supplier_channel_id)]);
            }
            $supplier_channel_id = implode(',',$supplier_channel_id);
            if($params['type_id']==5){
                $res = DB::update($table_field[0])->set(array('supplier_channel_id'=>$supplier_channel_id))->where("id={$params['product_id']}")->execute();
            }else{
                $ishidden = $params['ishidden'];
                $status = $ishidden==1?2:3;
                $res = DB::update($table_field[3])->set(array('supplier_channel_id'=>$supplier_channel_id,'ishidden'=>$ishidden,'status'=>$status))->where("id={$params['product_id']}")->execute();
            }
        }else{
            if($params['status']==0 || $params['status']==4){
                if($params['type_id']==5){
                    $res = DB::update($table_field[0])->set(array('status'=>1))->where("id={$params['product_id']}")->execute();
                }else{
                    $res = DB::update($table_field[3])->set(array('ishidden'=>0,'status'=>1))->where("id={$params['product_id']}")->execute();
                }
            }else{
                if($product['status']!=3 && $product['status']!=2){
                    exit(json_encode(array('status'=>false,'msg'=>'需平台审核通过后才可操作！')));
                }
                if($params['type_id']==5){
                    $ishidden = $params['ishidden'];
                    $status = $product['status']==2?3:2;
                    $res = DB::update($table_field[0])->set(array('status'=>$status,'ishidden'=>$ishidden))->where("id={$params['product_id']}")->execute();
                }else{
                    $ishidden = $params['ishidden'];
                    $status = $ishidden==1?2:3;
                    $res = DB::update($table_field[3])->set(array('ishidden'=>$ishidden,'status'=>$status))->where("id={$params['product_id']}")->execute();
                }
            }
        }

        echo json_encode(array(
            'status' => $res?true:false,
            'msg'    => '操作成功'
        ));
    }

    public function action_channel_set()
    {
        $channel = Model_Supplier::get_ota_channel($this->_supplier_id,true);
        $channel_status = DB::select()->from('supplier')->where("id={$this->_supplier_id}")->execute()->get('channel_status');
        $channel_status = json_decode($channel_status,true);
        foreach ($channel as &$v){
            if($channel_status[$v['id']]){
                $v['status'] = $channel_status[$v['id']];
            }else{
                $v['status'] = 2;
            }
        }
        $b2b_url = DB::select()->from('sysconfig')->where("varname='cfg_ebooking_domain_name'")->execute()->get('value');

        $this->assign('b2b_url',$b2b_url);
        $this->assign('channel',$channel);
        $this->display('channel/set');
    }

    public function action_ajax_channel_status()
    {
        $status = Arr::get($_POST,'status');
        $status = $status==1?2:1;
        $channel_id = Arr::get($_POST,'channel_id');
        $channel_status = DB::select()->from('supplier')->where("id={$this->_supplier_id}")->execute()->get('channel_status');
        $channel_status = $channel_status?json_decode($channel_status,true):[];

        if($channel_status[$channel_id]){
            $channel_status[$channel_id] = $status;
        }else{
            $new_channel_status = array(
                $channel_id => $status
            );
            $channel_status = $channel_status+$new_channel_status;
        }
        $ishidden = $status==1?0:1;
        Model_Supplier::update_product_sales_status($channel_status,$this->_supplier_id,$channel_id,$ishidden);
        $res = DB::update('supplier')->set(array('channel_status'=>json_encode($channel_status)))->where("id={$this->_supplier_id}")->execute();
        echo $res?1:2;
    }
}